<template>
  <div class="app-container">
    <div class="filter-container">
      <el-button v-waves class="filter-item" type="success" icon="el-icon-edit" @click="handleCreate">
        新增
      </el-button>
    </div>

    <el-table
      :key="tableKey"
      v-loading="listLoading"
      :data="list"
      border
      fit
      max-height="690"
      highlight-current-row
      style="width: 100%;"
      :row-style="{height:'32px'}"
      tooltip-effect="light"
    >
      <el-table-column label="等级序号" align="center" fixed>
        <template slot-scope="{row}">
          <span>{{ row.levelId }}</span>
        </template>
      </el-table-column>
      <el-table-column label="等级名称" align="center">
        <template slot-scope="{row}">
          <span>{{ row.levelName }}</span>
        </template>
      </el-table-column>
      <el-table-column label="所需经验" align="center">
        <template slot-scope="{row}">
          <span>{{ row.experience }}</span>
        </template>
      </el-table-column>
      <el-table-column label="最近修改时间" align="center">
        <template slot-scope="{row}">
          <span>{{ row.updateTime }} </span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" fixed="right">
        <template slot-scope="{row}">
          <el-button type="primary" size="mini" @click="handleUpdate(row)">
            编辑
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total>0" :total="total" :page-num.sync="listQuery.pageNum" :page-size.sync="listQuery.pageSize" @pagination="showLevelList" />

    <el-dialog :title="textMap[dialogStatus]" :visible.sync="dialogFormVisible" width="35%">
      <el-form ref="dataForm" :rules="rules" :model="temp" label-width="100px" style="width: 80%; margin-left:5%;">
        <el-form-item label="等级名称" prop="levelName">
          <el-input v-model="temp.levelName" />
        </el-form-item>
        <el-form-item label="所需经验" prop="experience">
          <el-input v-model="temp.experience" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">
          取消
        </el-button>
        <el-button type="primary" @click="saveData">
          确认
        </el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { showLevelList, saveLevel } from '@/api/rules-level'
import waves from '@/directive/waves' // waves directive
import Pagination from '@/components/Pagination'
import { isIntegerNature } from '@/utils/validate'

export default {
  name: 'Level',
  components: { Pagination },
  directives: { waves },
  data() {
    return {
      tableKey: 0,
      list: [],
      total: 0,
      listLoading: true,
      dialogLoading: false,
      listQuery: {
        pageNum: 1,
        pageSize: 20
      },
      temp: {
        levelId: undefined,
        levelName: '',
        experience: ''
      },
      dialogFormVisible: false,
      dialogStatus: '',
      textMap: {
        update: '编辑',
        create: '新增'
      },
      rules: {
        levelName: [{ required: true, message: '请填写等级名称', trigger: 'blur' }],
        experience: [{ required: true, message: '请填写所需经验值', trigger: 'blur' }, { validator: isIntegerNature, trigger: 'blur' }]
      }
    }
  },
  created() {
    this.showLevelList()
  },
  methods: {
    showLevelList() {
      this.listLoading = true
      showLevelList(this.listQuery).then(response => {
        this.list = response.data.list
        this.total = response.data.total
        setTimeout(() => {
          this.listLoading = false
        }, 100)
      })
    },
    handleFilter() {
      this.listQuery.page = 1
      this.showLevelList()
    },
    resetDialog() {
      this.temp = {
        levelId: undefined,
        levelName: '',
        experience: ''
      }
    },
    handleCreate() {
      this.resetDialog()
      this.dialogStatus = 'create'
      this.dialogFormVisible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].clearValidate()
      })
    },
    handleUpdate(row) {
      this.temp = Object.assign({}, row) // copy obj
      this.dialogStatus = 'update'
      this.dialogFormVisible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].clearValidate()
      })
    },
    saveData() {
      var text = ''
      if (this.dialogStatus === 'update') {
        text = '更新成功'
      } else {
        text = '新增成功'
      }
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          saveLevel(this.temp).then(() => {
            this.showLevelList()
            this.dialogFormVisible = false
            this.$notify({
              title: 'Success',
              message: text,
              type: 'success',
              duration: 2000
            })
          })
        }
      })
    }
  }
}
</script>
